<?php
class Users extends BaseBean{
  	
  	protected $_table= "tb_users";
    protected $_primarykey= "id";
    protected $_table_alias= "a";
    protected $powers = null;

    
    public function generateSql(){
        return "select a.*,b.name as shop,b.wechat_id from tb_users a left join tb_shop b on a.departmentid=b.id";
    }


    public function getPowers(){
        
        if($this->powers == null){
            $this->loadPowers();
        } 
        return $this->powers;
    }


    private function loadPowers(){
        $role_ids = $this-> getRoles();
        if($role_ids!= null && $role_ids!=""){
           $filter = new SqlFilter();
           $filter->addExpress("id in (".$role_ids.")");
           $rows = AbstractBean::LoadBean("Roles")->fetchArray($filter);  
           $powerIds="";
           if($rows!=null ){
              for($i=0;$i<count($rows);$i++){
                  $spit =  $powerIds == ""?"":",";
                  $powerIds.=$spit.$rows[$i]["power_ids"];      
              }
           }
           if($powerIds!=""){
              $filter = new SqlFilter();
              $filter->addExpress("id in (".$powerIds.")");
              $this->powers = array();
              $rows = AbstractBean::LoadBean("Powers")->fetchArray($filter);  
              for($i=0;$i<count($rows);$i++){
                 $this->powers[$rows[$i]["code"]] = $rows[$i]["name"];
              }
           }

        }

    }



    public function getRoles(){
        return  $this->getStringValue("role_ids");         
    }



    public static function GetLoginUser(){
      	$user = null;
      	if(!empty($_SESSION["curt_user"])){
      		$user = $_SESSION["curt_user"];
      	}
      	
      	return $user;

    }

    public static function GetLoginUserPowers(){
        $power = array();
        if(!empty($_SESSION["powers"])){
           $power = $_SESSION["powers"];
        }
        return $power;

    }

    public static function HavePermission($power){
        $powers = $_SESSION["powers"];
        if($powers==null) {
            return false;
        }
        return array_key_exists($power,$powers);
    }


    public static function GetLoginUserID(){
  		  $userId  = null;
    		if(!empty($_SESSION["user_id"])){
      		$userId = $_SESSION["user_id"];
      	}	
      	return $userId;
    }

    public static function GetLoginShopID(){
    		$shopId  = null;
    		if(!empty($_SESSION["shop_id"])){
      		$shopId = $_SESSION["shop_id"];
      	}	
      	return $shopId;	  	
    }
     public static function GetLoginShopName(){
        $shop  = null;
        if(!empty($_SESSION["shop"])){
          $shopId = $_SESSION["shop"];
        } 
        return $shop;     
    }

     public static function GetLoginCommpanyID(){
      $companyId  = null;
      if(!empty($_SESSION["company_id"])){
        $companyId = $_SESSION["company_id"];
      } 
      return $companyId;     
    }




    


    
  
  
}
?>